﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class findchild2 : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
           // Response.Redirect("~/cal.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            BindData();
        }
        protected void BindData()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            scon.Open();

            SqlCommand cmd = new SqlCommand("SELECT CID,FirstName,LastName FROM Person where CID=@text or FirstName=@text or Lastname=@text", scon);
            cmd.Parameters.AddWithValue("@text", TextBox1.Text);
            SqlDataReader reader = cmd.ExecuteReader();


            GridView1.DataSource = reader;
            GridView1.DataBind();
            scon.Close();
        }
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {

        }

        protected void BindDatadek()
        {
            SelectStat.Text = string.Empty;
            bool selectorChecked3 = false;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("CID");
            dt2.Columns.Add("FirstName");
            dt2.Columns.Add("LastName");



            //   dt2.Columns.Add("note");
            foreach (GridViewRow row in GridView1.Rows)
            {
                RadioButton cb = (RadioButton)row.FindControl("CaseSelector3");
                if (cb != null && cb.Checked)
                {
                    selectorChecked3 = true;

                    DataRow dr2;
                    dr2 = dt2.NewRow();
                    string CID = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    string FirstName = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string LastName = GridView1.Rows[row.RowIndex].Cells[2].Text;
                    SelectStat.Text += string.Format(CID);
                    Label2.Text = string.Format(FirstName);
                    Label3.Text = string.Format(LastName);
                    dt2.Rows.Add(CID, FirstName, LastName);


                }
            }
            SelectStat.Visible = selectorChecked3;


        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }





        protected void checkcaseno()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT CID from Evaluation where CID = @sj", scon);
            cmd.Parameters.AddWithValue("@sj", SelectStat.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() != false)
            {
                
                // crepsnumber();
                // psnumbertolabel();
                sendsession();
                Bindcase();
                sendsession2();
            }

            else
            {

                sendsession();
                Response.Redirect("~/cal.aspx");

            }
            reader.Close();

        }



        protected void CaseSelector3_CheckedChanged(object sender, EventArgs e)
        {


            BindDatadek();
            checkcaseno();
            
            
            

        }

        protected void sendsession()
        {
            Session["CID"] = SelectStat.Text;
            Session["FirstName"] = Label2.Text;
            Session["LastName"] = Label3.Text;


            Session["CaseNo"] = Label6.Text;


          //  Response.Redirect("~/diagnosemedical.aspx");



        }

        protected void sendsession2()
        {
            


            Session["CaseNo"] = Label6.Text;


            Response.Redirect("~/Choosing.aspx");



        }



        //สร้าง case จาก case มากสุด
        protected void Bindcase()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            SqlCommand cmd2 = new SqlCommand("SELECT [CID],Max(CaseNo) as caseno FROM [Evaluation] where CID=@CID group by CID", scon);


            scon.Open();
            cmd2.Parameters.AddWithValue("@CID", SelectStat.Text);

            SqlDataReader reader2 = cmd2.ExecuteReader();



            if (reader2.Read())
            {

                Label6.Text = reader2["caseno"].ToString();

            }
            else
            {
                //MessageBox("ไม่พบเลขบัตรครับ");
            }
            scon.Close();
            reader2.Close();


        }



    }
}